package com.tv.enoguia.fragment;

import java.util.ArrayList;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.tv.enoguia.R;
import com.tv.enoguia.adapter.ListCategoriesAdapter;
import com.tv.enoguia.database.MySQLiteHelper;

public class ListScr extends Fragment {
	
	TextView title;
	ListView listCategories;
	ListCategoriesAdapter adapter;
//	ArrayList<Categories> categories = new ArrayList<Categories>();
	ArrayList<String> categories = new ArrayList<String>();
	MySQLiteHelper db;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		db = new MySQLiteHelper(getActivity());
		
		/*if (MainActivity.getCurrentTab() == 0){
			finish();
		}*/
		
	}
	
	
	
	ViewGroup v;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		if(v==null) {
			v = (ViewGroup) inflater.inflate(R.layout.scr_list_categories, container, false);
		} else {
			((ViewGroup) v.getParent()).removeView(v);
		}
		
		
		title = (TextView) v.findViewById(R.id.title);
		listCategories = (ListView) v.findViewById(R.id.list_categories);
		
		dataDemo();
		adapter = new ListCategoriesAdapter(getActivity(), categories);
		listCategories.setAdapter(adapter);
		init();
		
		
		listCategories.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int positon,
					long arg3) {
				/*Intent t = new Intent(getActivity(), ListBussinessActivity.class);
				t.putExtra("category", categories.get(positon));
				startActivity(t);*/
				 Fragment productDetailFragment = ListBussinessFragment.newInstance();
				  FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
				  transaction.addToBackStack(null);
				  Bundle b = new Bundle();
				  b.putString("category",  categories.get(positon));
				  productDetailFragment.setArguments(b);
				  transaction.replace(R.id.replace, productDetailFragment).commit();
			}
			
		});
		
		
		return v;
	}




	void dataDemo(){
		/*categories.add(new Categories("DO Catalunya"));
		categories.add(new Categories("DO Conca de Barbera"));
		categories.add(new Categories("DO Montsant"));
		categories.add(new Categories("DO Penedes"));
		categories.add(new Categories("DOQ Priorat"));
		categories.add(new Categories("DO Tarragona"));
		categories.add(new Categories("DO Terra Alta"));*/
		categories = db.getCategories();
	}
	
	
	void init(){
		
		String language = Locale.getDefault().getISO3Language().toString(); 
		getString(R.string.denomimos);
		if (language.equals("eng")){
			title.setText("Denominations");
		} else if (language.equals("cat")) {
			title.setText("Denominaciones");
		}else if (language.equals("spa")) {
			title.setText("Denominacions");
		} else if (language.equals("vie")) {
			title.setText("Denominacions");
		}
	}
}
